home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / admin / linuxcon.000 / linuxcon / linuxconf-1.6 / xconf / adaptor.c next >
C/C++ Source or Header  |  1995-01-06  |  4KB  |  161 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <limits.h>
  5. #include <sys/stat.h>
  6. #include "../misc/misc.h"
  7. #include "xconf.h"
  8. #include "components.h"
  9.  
  10. #if 0
  11. void xconf_opt2opt2(
  12.     char *tbopt[],
  13.     int nbopt,
  14.     char *tbopt2[])
  15. {
  16.     int nb2 = 0;
  17.     /* #Specification: files / adaptor.conf & screen.conf
  18.         Each configuration line in adaptor.conf is interpreted this way.
  19.         If it start in first column, then the first word identify the
  20.         supplier and the rest of the line identify a specific product.
  21.  
  22.         If it start with a white character (space, tabs), it is assume
  23.         that it is another product of the same supplier and the supplier
  24.         name won't be repeated.
  25.  
  26.         This allows presentation like this
  27.  
  28.             ibm        model 25
  29.                     model 30
  30.             ati        mach32 ultra
  31.                     mac32 ultra
  32.  
  33.         If there is only one word in the line, then it will be shown
  34.         in the first column of the selection list.
  35.     */
  36.     for (int i=0; i<nbopt; i++){
  37.         char *opt = tbopt[i];
  38.         char *opt1 = " ";
  39.         char *opt2 = " ";
  40.         if (isspace(opt[0])){
  41.             char *pt = opt;
  42.             while (isspace(*pt)) pt++;
  43.             opt2 = pt;
  44.         }else{
  45.             char *pt = opt;
  46.             while (*pt > ' ') pt++;
  47.             if (isspace (*pt)){
  48.                 *pt++ = '\0';
  49.                 while (isspace(*pt)) pt++;
  50.                 opt1 = opt;
  51.                 opt2 = pt;
  52.             }else{
  53.                 opt1 = opt;
  54.             }
  55.         }
  56.         tbopt2[nb2++] = opt1;
  57.         tbopt2[nb2++] = opt2;
  58.     }
  59.     tbopt2[nb2] = NULL;
  60. }
  61. #endif
  62. /*
  63.     Read many (potentially none) component files in a directory dir
  64.     using wildcard wild.
  65.     Return the number of files read.
  66.     Return -1 if any kind of error.
  67. */
  68. int xconf_readconf(
  69.     COMPONENTS &comp,
  70.     const char *dir,
  71.     const char *wild)
  72. {
  73.     int ret = 0;
  74.     int err = 0;
  75.     char cmd[300];
  76.     sprintf (cmd,"echo %s/%s",dir,wild);
  77.     FILE *fin = popen (cmd,"r");
  78.     if (fin != NULL){
  79.         char buf[PATH_MAX];
  80.         while (fgets (buf,sizeof(buf)-1,fin)!=NULL){
  81.             str_strip (buf,buf);
  82.             struct stat sbuf;
  83.             if (buf[0] != '\0' && stat(buf,&sbuf)!=-1){
  84.                 ret++;
  85.                 err |= comp.read (buf);
  86.             }
  87.         }
  88.         pclose (fin);
  89.     }else{
  90.         xconf_error ("Can't execute command\n%s\n(%s)\n",cmd
  91.             ,strerror(errno));
  92.     }
  93.     return err == 0 ? ret : -1;
  94. }
  95. /*
  96.     Read many (potentially none) component files in the current directory
  97.     and in the USR_LIB_XCONF directory using wildcard wild.
  98.     Return the number of files read.
  99.     Return -1 if any kind of error.
  100. */
  101. int xconf_readconfall(
  102.     COMPONENTS &comp,
  103.     const char *wild1,
  104.     const char *wild2)
  105. {
  106.     /* #Specification: data files / locations
  107.         Configuration file (database files) are search in the current
  108.         directory and in the USR_LIB_XCONF directory. The first one
  109.         is there mostly to help people experiment with xconf.
  110.  
  111.         All files located are merged and sorted in memory.
  112.  
  113.         The merge is mostly an addition. No precedence is done
  114.         to eliminate duplicated entries.
  115.     */
  116.     int ret1 = xconf_readconf (comp,".",wild1);
  117.     int ret2 = xconf_readconf (comp,".",wild2);
  118.     int ret3 = xconf_readconf (comp,USR_LIB_XCONF,wild1);
  119.     int ret4 = xconf_readconf (comp,USR_LIB_XCONF,wild2);
  120.     comp.sort();
  121.     int ret = ret1 + ret2 + ret3 + ret4;
  122.     if (ret1 == -1 || ret2 == -1 || ret3 == -1 || ret4 == -1){
  123.         ret = -1;
  124.     }
  125.     return ret;
  126. }
  127. /*
  128.     Select an adaptor type and program the different option necessary.
  129. */
  130. int xconf_adaptor (XCONFIG &xcfg)
  131. {
  132.     COMPONENTS comp;
  133.     xconf_readconfall (comp,"adaptors.xconf","adaptors.*.xconf");
  134.     char *tbopt2[4001];
  135.     int nbopt = comp.setmenu (tbopt2);
  136.     int ret = 0;
  137.     if (nbopt > 0){
  138.         int choice = 0;
  139.         xconf_menu ("Adaptor list"
  140.             ,"You must select an adaptor from this list\n"
  141.              "The list is sorted by manufacturer\n"
  142.              "and product"
  143.             ,tbopt2,choice);
  144.         if (choice != -1){
  145.             COMPONENT *compo = comp.item(choice);
  146.             xcfg.addcomment ("Adaptor",compo->keyw,compo->arg);
  147.             xcfg.merge (compo->xconfig);
  148.             xconf_notice (compo->notice);
  149.         }
  150.     }else{
  151.         xconf_error (
  152.             "No sadaptors.xconf file could be found\n"
  153.             "either in the current directory or in\n"
  154.             "the directory %s\n"
  155.             ,USR_LIB_XCONF);
  156.         ret = -1;
  157.     }
  158.     return ret;
  159. }
  160.  
  161.